package com.baize.baizeapp

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.baize.baizeapp.test.MohuActivity
import com.baize.baizeapp.test.RecyclerViewActivity
import com.baize.baizeapp.test.album.AlbumActivity
import com.baize.baizeapp.test.chat.ChatActivity
import com.baize.baizeapp.test.sku.SkuActivity
import com.baize.baizeapp.test.video.VideoActivity
import com.baize.baizeapp.test.widget.HSCountSelector
import com.baize.baizeapp.utils.SPUtils
import com.tencent.mmkv.MMKV
import com.tencent.mmkv.MMKVLogLevel
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.Exception
import java.net.HttpURLConnection
import java.net.URL
import java.security.SecureRandom
import javax.crypto.Cipher
import javax.crypto.SecretKey
import javax.crypto.spec.SecretKeySpec
import kotlin.concurrent.thread


class MainActivity : AppCompatActivity() {
    lateinit var tvText: TextView
    lateinit var mMkv: MMKV
    lateinit var sp: SharedPreferences

    lateinit var countSelector:HSCountSelector

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tvText = findViewById<TextView>(R.id.tv_text)
        countSelector = findViewById(R.id.countSelector)

        MMKV.initialize(this, MMKVLogLevel.LevelDebug)
        mMkv = MMKV.mmkvWithID("baize_mmkv", MMKV.SINGLE_PROCESS_MODE)!!

        sp = getSharedPreferences("baize_sp", Context.MODE_PRIVATE)
    }

    fun saveSP(view: View) {
        val start = System.currentTimeMillis()
        repeat(1000) {
            sp.edit()
                .putString("name${it}", "baize")
                .commit()
        }
        tvText.text = "saveSP: SP耗时：${System.currentTimeMillis() - start}"
    }

    fun saveMMKV(view: View) {
        val start = System.currentTimeMillis()
        repeat(1000) {
            mMkv.encode("name${it}", "baize")
        }
        tvText.text = "saveMMKV: MMKV耗时：${System.currentTimeMillis() - start}"
    }

    val password = "648555"
    val text = "123456"
    val isEncrypt = true

    init {
//        SecretKeyFactory.getInstance()
//        Security.addProvider(BouncyCastleProvider())
    }

    fun jiami(v: View) {
//        Log.i("yanze", "jiami: 密文：${encrypt(text, password)}")
        val keySpec = SecretKeySpec(password.toByteArray(), "DES")
        val cipher: Cipher = Cipher.getInstance("DES/ECB/PKCS5Padding")
        val random = SecureRandom()
        cipher.init(
            if (isEncrypt) Cipher.ENCRYPT_MODE else Cipher.DECRYPT_MODE,
            keySpec,
            random
        )
        val cipherByte = cipher.doFinal(text.toByteArray())
        Log.i("yanze", "jiami: $cipherByte 密文:${String(cipherByte)}")
    }

    //相册
    fun album(v: View) {
        AlbumActivity.open(this)
    }

    fun mohu(v: View) {
        MohuActivity.open(this)
    }

    fun playVideo(v: View) {
        VideoActivity.open(this)
    }

    fun skuSelect(v: View) {
        SkuActivity.open(this)
    }

    fun requestNet(v: View) {
        thread {
            var connection: HttpURLConnection? = null
            try {
                val response = StringBuilder()
                val url = URL("https://wanandroid.com/wxarticle/chapters/json")
                connection = url.openConnection() as HttpURLConnection
                val input = connection.inputStream
                val reader = BufferedReader(InputStreamReader(input))
                reader.use {
                    reader.forEachLine {
                        response.append(it)
                    }
                }
                Log.d("yanze",response.toString())
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                connection?.disconnect()
            }
        }
    }

    fun recyclerViewTest(v: View) {
        RecyclerViewActivity.open(this)
    }

    fun chatModel(v: View) {
        ChatActivity.open(this)
    }

    fun encrypt(data: String, password: String): String {
        val plaintext: ByteArray = data.toByteArray()
//        keygen.init(56)
        val key: SecretKey = SecretKeySpec(password.toByteArray(), "DES")
//        val key: SecretKey = keygen.generateKey()
        val cipher = Cipher.getInstance("DES")
        cipher.init(Cipher.ENCRYPT_MODE, key)
        val ciphertext: ByteArray = cipher.doFinal(plaintext)
        val iv: ByteArray = cipher.iv
        return String(ciphertext)
    }
}